package com.sky.controller.admin;


import com.sky.annotation.ActionLog;
import com.sky.dto.CateDTO;
import com.sky.dto.PageQuery;
import com.sky.entity.Category;
import com.sky.entity.FirstCategory;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.impl.CategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@Api(tags = "管理端-分类的相关接口")
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryServiceImpl categoryService;

    /**
     * 获取分类信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取分类信息")
    public Result<List<FirstCategory>> list() {
        List<FirstCategory> list = categoryService.categories();
        return Result.success(list);
    }
    /**
     * 获取一级分类信息
     * @return
     */
    @GetMapping("/first")
    @ApiOperation("获取一级分类信息")
    public Result<PageResult> first(PageQuery pageQuery) {
        PageResult result = categoryService.first(pageQuery);
        return Result.success(result);
    }

    /**
     * 获取二级分类信息
     * @return
     */
    @GetMapping("/second")
    @ApiOperation("获取二级分类信息")
    public Result<PageResult> second(PageQuery pageQuery) {
        PageResult second = categoryService.second(pageQuery);

        return Result.success(second);
    }
    /**
     * 获取三级分类信息
     * @return
     */
    @GetMapping("/third")
    @ApiOperation("获取三级分类信息")
    public Result<PageResult> third(PageQuery pageQuery) {
        PageResult third = categoryService.third(pageQuery);
        return Result.success(third);
    }

    /**
     * 添加分类
     * @param cateDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加分类")
    @ActionLog(value = "添加了分类",module = "分类管理模块")
    public Result add(@RequestBody CateDTO cateDTO){
        categoryService.saveByType(cateDTO);
        return Result.success();
    }

    /**
     * 根据分类id获取详情
     * @param type
     * @param id
     * @return
     */
    @GetMapping("/detail/{type}/{id}")
    @ApiOperation("根据分类id获取分类的详情")
    public Result<Category> detail(@PathVariable("type")Integer type,@PathVariable("id") Integer id){
      Category category=categoryService.detail(type,id);
      return Result.success(category);
    }

    /**
     * 修改分类
     * @param
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    @ActionLog(value = "修改了分类",module = "分类管理模块")
    public Result update(@RequestBody CateDTO cateDTO){
        categoryService.updateByType(cateDTO);
        return Result.success();
    }
    /**
     * 根据id删除分类
     * @param type
     * @param id
     * @return
     */
    @DeleteMapping("/{type}/{id}")
    @ApiOperation("根据分类id删除分类")
    @ActionLog(value = "删除了分类",module = "分类管理模块")
    public Result delete(@PathVariable Integer type,@PathVariable("id") Integer id){
        categoryService.deleteByType(type,id);
        return Result.success();
    }
}
